'use strict'

const {logger} = require('ee-core/log')
const {dialog, nativeImage, BrowserWindow} = require('electron')
const {getBaseDir} = require('ee-core/ps')
const path = require('node:path')

/**
 * 原生弹窗
 * @author clevstn
 */
class NativeBoxController {
    #getStateNativeImage(type = 'none') {
        let stateFilename
        switch (type) {
            case 'info':
                stateFilename = 'info.png'
                break
            case 'error':
                stateFilename = 'error.png'
                break
            case 'question':
                stateFilename = 'question.png'
                break
            case 'warning':
                stateFilename = 'warning.png'
                break

        }
        return stateFilename ? nativeImage.createFromPath(
            path.join(getBaseDir(), 'public', 'images', stateFilename)
        ) : null
    }

    /**
     * 展示弹窗 (ipc)
     */
    showBox({title, detail, type = 'none'}, event) {
        const currentWindow = BrowserWindow.fromWebContents(event.sender)
        const options = {
            type: type,
            title,
            detail: detail
        }
        const nativeIcon = this.#getStateNativeImage(type)
        if (nativeIcon != null) {
            options.icon = nativeIcon
        }
        dialog.showMessageBox(currentWindow, options)
        logger.debug(NativeBoxController.toString(), detail)
    }

    /**
     * 选择目录路径
     */
    async selectDir(args, event) {
        const currentWindow = BrowserWindow.fromWebContents(event.sender)
        return await dialog.showOpenDialog(currentWindow, {
            title: args.title,
            properties: ['openDirectory', 'createDirectory']
        })
    }
}

NativeBoxController.toString = () => '[class NativeBoxController]'
module.exports = NativeBoxController